<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          收款管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="ser.key" placeholder="项目名称" style="width: 260px" class="handle-input mr10">
          <template #prepend>项目名称</template>
        </el-input>
        <el-date-picker v-model="ser.start" type="date" placeholder="开始日期" value-format="YYYY-MM-DD" class="handle-input mr10"></el-date-picker>
        <el-date-picker v-model="ser.end" type="date" placeholder="结束日期" value-format="YYYY-MM-DD" class="handle-input mr10"></el-date-picker>
        <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
        <el-button @click="addClick">添加</el-button>
      </div>
      <el-table :data="datas" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="code" label="收款编号" width="150"></el-table-column>
        <el-table-column prop="projectName" label="项目名称"></el-table-column>
        <el-table-column prop="customerName" label="客户名称"></el-table-column>
        <el-table-column prop="receiveTypeName" label="收款方式" width="150"></el-table-column>
        <el-table-column prop="money" label="收款金额" width="120" :formatter="(...arr) => arr[2] + '元'"></el-table-column>
        <el-table-column prop="receiveDate" label="收款日期" v-if="bigScreen" :formatter="dateformatter" width="100"></el-table-column>
        <el-table-column prop="salerName" label="经办人" v-if="bigScreen" width="120"></el-table-column>
        <el-table-column prop="orgName" label="部门名称" width="120"></el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-magic-stick" @click="handleEdit(scope.row)">预览</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="ser.page" :page-size="ser.row" :total="total" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <form-add ref="r_add" @search="search" />
    <form-view ref="r_view" />
  </div>
</template>
<script>
import FormAdd from './Add.vue'
import FormView from './View.vue'
export default {
  components: { FormAdd, FormView },
  data() {
    return {
      ser: {
        page: 1,
        row: 15,
        key: '',
        start: '',
        end: '',
      },
      datas: [],
      total: 0,
      bigScreen: window.innerWidth > 768,
    }
  },
  methods: {
    addClick() {
      this.$refs.r_add.showdata()
    },
    searchClick() {
      this.handlePageChange(1)
    },
    handlePageChange(page) {
      this.ser.page = page
      this.search()
    },
    search() {
      this.$api.receivemoney.search(this.ser).then(({ msg: { items, total } }) => {
        this.datas = items
        this.total = total
      })
    },
    dateformatter(...arr) {
      return arr[2].slice(0, 10)
    },
    handleEdit(row) {
      this.$refs.r_view.show(row.id)
    },
    handleDelete(row) {
      this.$confirm('是否确定要删除当前还款记录')
        .then(() => {
          this.$api.receivemoney.remove(row.id).then(({ state, msg }) => {
            this.$message[state ? 'success' : 'warning'](msg)
            if (state) this.search()
          })
        })
        .catch(() => {})
    },
  },
  mounted() {
    this.searchClick()
  },
}
</script>
